Repository navigation
fix: 8/16/32-bit histograms throw on count overflow instead of wrapping - #77
Merged
Merged
Conversation
alexvictoor
force-pushed
the
fix/count-overflow
branch
from
November 15, 2026 21:10
c1adb76 to
7e7e5e9
Compare
8/16/32-bit count storage is unsigned, so a count past the cell width wrapped silently (255 + 1 -> 0 in an 8-bit JS array, 2^32 -> 0 in the 32-bit WASM build), and a negative count was stored as a huge value or dropped. The JS `newCount < 0` check could never fire for unsigned arrays. Writes now range-check against the cell width, as Java's ShortCountsHistogram and IntCountsHistogram do, and throw "<n> would overflow <bits>-bit integer count" or "<n> would underflow <bits>-bit integer count". The check runs before the write, so the offending cell is never modified. 64-bit, Float64 and packed storage keep their previous behaviour. WASM aborts now carry the AssemblyScript message instead of only the source location, which optimized builds do not have. Known limitation: add/subtract of same-shaped histograms are not atomic. An overflow part-way through leaves the cells before the failing one applied and totalCount unchanged, as Java's add does. Pre-validating the whole range was measured at 5-7x slower on add and was not kept. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
- Restore @inline on recordCountAtValue; recordCountAtValueFromF64 is undecorated - Remove the duplicated @ts-ignore in incrementCountAtIndex - WASM 64-bit: subtract underflow throws (scalar and vector paths, u64 lanes compared unsigned via a sign bias) - WASM: a negative count throws for every width, instead of saturating to 0 for u64 - Typed array histograms check the count before an auto-resize, so a rejected count leaves highestTrackableValue unchanged - Tests: rename the single-cell add/subtract tests, add Java-semantics tests (earlier cells stay applied when a later cell throws), add 64-bit WASM tests Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
alexvictoor
force-pushed
the
fix/count-overflow
branch
from
November 21, 2026 14:20
7e7e5e9 to
1f2a816
Compare
alexvictoor
marked this pull request as ready for review
November 22, 2026 14:56
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixed-width count storage (8/16/32-bit, JS typed arrays and the AssemblyScript
Histogram8/16/32) now range-checks each write and throws instead of wrapping, matching Java'sShortCountsHistogram/IntCountsHistogram.flowchart TD K["SIMD add/subtract block"] --> C{"any lane wraps? (lane-wise lt_u)"} C -- no --> V["vector store"] C -- yes --> W W["write to a cell: increment, addToCountAtIndex, setCountAtIndex"] --> N["newCount = cell + delta, in a wide type"] N --> O{"newCount above the width's max?"} O -- yes --> E1["throw: n would overflow 8/16/32-bit integer count"] O -- no --> U{"newCount below 0?"} U -- yes --> E2["throw: n would underflow 8/16/32-bit integer count"] U -- no --> S["store newCount"]On the WASM side the 64-bit histogram also throws on subtract underflow (it wrapped to about 2^64) and on a negative count (it was dropped by the u64 cast); the JS Float64 and packed histograms accept negative counts as before. An auto-resizing JS histogram checks the count against its width before growing, so a rejected
recordValueWithCountleaveshighestTrackableValueunchanged. The WASM histograms do not have that pre-check yet: an 8-bit WASM histogram with autoResize grows from 1000 to 1,000,000 before rejectingrecordValueWithCount(1000000, 300)(known gap, found in review). The WASM abort hook now surfaces the AssemblyScript message instead of only "Abort in WebAssembly at L:C".Java semantics, kept on purpose: an add/subtract that fails part-way leaves the earlier cells applied and
totalCountunchanged, exactly as Java'sadd. Pre-validating the whole range cost 5-7x on add, so it was not kept; tests document the partial application instead.Evidence
Before: a probe on master (
setCountAtIndex(i, 255)thenrecordValueWithCount(v, 1)):After:
Failing-then-passing test (pseudocode):
h.setCount(42, MAX); expect(() => h.record(42, 1)).toThrow("would overflow 8-bit"); expect(h.summary).toEqual(before); thenh.setCount(42, MAX - 1); h.record(42, 1)succeeds.Bench (
npx vitest bench --run --reporter verbose, ops/s, two runs each where noted; noise is about ±5-10%):Recording is within noise. Add is unchanged within noise after the SIMD break-out; the scalar pre-validation variant was 105k (WASM 32) and 9k (Int32) and was dropped.
After (tests):
npm test384 passed after the rebase on master 41cc6e6 (324 before it), including the partial-application cases on add and subtract, the 64-bit WASM underflow and negative-count cases, and the autoResize pre-check;npm run astest:cipasses with the new AS boundary tests;npm run typecheckclean. The add bench was re-run before and after the review follow-ups: WASM 32B 611k to 631k before, 617k to 636k after.Merge Danger
Door: two-way
Reverting restores silent wrapping; no data or format changes.
Blast Radius: moderate
Code that relied on wrapping, or on negative counts being dropped in WASM, will now throw, on 64-bit WASM histograms too. The throw is the intended behaviour, but callers that recorded large counts into 8/16-bit histograms will see exceptions where they previously got garbage. Thrown values for the WASM path are now
Errorobjects with the message (they wereAbort in WebAssembly at L:C). The JS path now throwsErrorobjects rather than strings.Overlaps #26 (external, older fix for integer overflows): if this merges, #26 can be closed.
🤖 Generated with Claude Code